﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

namespace TradeForm
{
    public partial class FrmCompanyInfo : Form
    {
        DataDBDataContext dbcntxt = new DataDBDataContext
                    (ConfigurationManager.ConnectionStrings["TradeForm.Properties.Settings.dbConnectionString"].ConnectionString);

        public FrmCompanyInfo()
        {
            InitializeComponent();
        }

        private void FrmCompanyInfo_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“dsComInfo.CompanyInfo”中。您可以根据需要移动或移除它。
            companyInfoTableAdapter.Connection = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings["TradeForm.Properties.Settings.dbConnectionString"].ConnectionString);
            this.companyInfoTableAdapter.Fill(this.dsComInfo.CompanyInfo);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            CompanyInfo com = null;
            var cominfo = (from good in dbcntxt.CompanyInfo
                           select good).ToList();
            try
            {
                if (cominfo.Count > 0)
                {
                    foreach (CompanyInfo item in cominfo)
                    {
                        dbcntxt.CompanyInfo.DeleteOnSubmit(item);
                    }
                }
            }
            catch
            { }
            try
            {
                com = new CompanyInfo();

                com.CAddress = this.txtCAddress.Text;
                com.CName = this.txtCName.Text;
                com.EAddress = this.txtEAddress.Text;
                com.EName = this.txtEName.Text;
                com.PostCode = this.txtPostCode.Text;
                com.Tel = this.txtTel.Text;
                com.DeclareCompany = this.txtDeclareCompany.Text;
                com.MembersDeclaration = this.txtMembersDeclaration.Text;
                com.TypeCompany = this.txtTypeCompany.Text;
                com.Typer = this.txtTyper.Text;

                dbcntxt.CompanyInfo.InsertOnSubmit(com);
                dbcntxt.SubmitChanges();
                MessageBox.Show("保存成功。");
            }
            catch (Exception ex)
            {
                MessageBox.Show("保存失败。");
                return;
            }

        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
